<?php
	include(ROOTPATH .'include'. DS .'protect.php');
	include ROOTPATH .'include'. DS .'isMyQuery.php';
	include(ROOTPATH .'include'. DS .'checkUserLogin.php');
	$powder_min = 5 ;
	include(ROOTPATH .'include'. DS .'checkPowder.php');
	include(ROOTPATH .'class'. DS .'task.class.php');
	$tag = new tag() ;
	$task = new task() ;
	
	$task_id			= getId('task_id');

	
	
	$fileName			= getQuery('fileName');
	$tagCustomMenu		= getQuery('tagCustomMenu');
	$thumb				= getQuery('thumb');
	$mediaSource		= getQuery('mediaSource');
	$mediaSourceTitle	= getQuery('mediaSourceTitle');
	$description		= getQuery('description');
	$tagCustomMenu		= getQuery('tagCustomMenu');	
	$timeEnd			= getQuery('timeEnd');


	if( $fileName == '' )
		exit( '{"status":"error","msg":"请填写任务标题."}');
	if( $timeEnd == '' )
		exit( '{"status":"error","msg":"请填写任务结束时间."}');
	
	// 移动	 ， 不信任客户端
	if( '' != $mediaSource ){
		$mediaSource = explode('|',$mediaSource);
		$mediaSource = moveUploadFile($mediaSource, TASK_FILE_PATH );	
	}
 	//$mediaThumbPhoto != MEDIA_THUMB && $mediaThumbPhoto != ''
	
	
	
	 
	if( '' != $thumb && $thumb != TASK_THUMB ){
		$thumb = explode('|',$thumb);		
		$thumb = moveUploadFile($thumb, TASK_FILE_PATH );
	}else{
		$thumbs = explode('|',$mediaSource);
		foreach($thumbs as $thumb){
			$fileType = getFileExt($thumb);
			if( $fileType == 'jpg' || $fileType == 'png' ){
				$mediaThumbPhoto = explode('|',$thumb);
				$mediaThumbPhoto = moveUploadFile($mediaThumbPhoto, TASK_FILE_PATH ,'copy' );	
				break ;
			}			
		}
		//exit($mediaThumbPhoto);
		if( '' == $mediaThumbPhoto)
			$mediaThumbPhoto = TASK_THUMB ;
		else{
			$saveName = date('His') . rand(1000,9999) ;
			$savePath = TASK_FILE_PATH . creatFolder(TASK_FILE_PATH);
			$newPhoto = new userUploadPhoto() ;
			$mediaThumbPhoto = $newPhoto -> cropUserPhoto($mediaThumbPhoto, $savePath , $saveName , TASK_THUMB_WIDTH , TASK_THUMB_HEIGHT  );
		}
		$thumb = $mediaThumbPhoto;
	}
	//exit( '{"status":"error","msg":"'.$thumb.'."}');
	
	
	
	//$str = array('-',' ',':');
	//$timeEnd = str_replace($str,"",$lists['endTime']);
	
	//tagCustomMenus parentId = 3;	
	// 火炬 ，
	
	$array['name'] 				= $fileName ;
	if($thumb)
		$array['thumb'] 			= $thumb ;
	$array['tag'] 				= $tagCustomMenu ;
	$array['description'] 		= $description;
		
	$array['timeEnd'] 			= $timeEnd ;
	$array['file'] 				= $mediaSource ;
	$array['fileTitle'] 		= $mediaSourceTitle ;	
	$array['created']			= date('Y-m-d H:i:s',time());
	$array['ip'] 				= ip() ;
	
	
	if( !$task_id ){
		$array['user_id'] 			= $user -> id();
		$newId = $dbs -> insert( DB_PRE.'task' ,$array , 'id' );
	}else{
		$array['updated_id'] 	= $user -> id();
		$array['updated'] 		= time();
		
		$lists = $dbs -> select('SELECT * FROM `'.DB_PRE.'task` WHERE `id` = '. $task_id .' and `user_id` = '. $user -> id() );
		$lists = $lists[0];
		if( (count($lists) == 0 || $task -> isTimeout($lists['timeEnd'],$task_id) ) && $user -> powder() < 9)
			exit('{"status":"error","msg":"任务过期,您无权修改."}');
		
		$dbs -> update( DB_PRE.'task' ,$array , ' `id` =  '. $task_id );
		$newId = $task_id ;
	}
	$array = array();
	
	
	$tagCustomMenu = $tag -> saveCustomMenu($tagCustomMenu);
	$tagCustomMenu = '3!'. $tagCustomMenu;
	$tagMenus = $tag -> saveTagMenu($tagCustomMenu,$newId,'task');
	$array['tag']	= $tagMenus;
	
	$dbs -> update( DB_PRE .'task' , $array , ' id = '. $newId );
	$newId = $encrypt -> code($newId);

	exit('{"status":"ok","url":"'. urlRewrite('option=task&id='.$newId ) .'","msg":'. json_encode( '成功发布. <a href="'.urlRewrite('option=taskAdd').'">发布新任务</a> , <a href="'. urlRewrite('option=task&id='+ $newId ) .'">查看</a>'  ) .'}');
	//
?>